package com.wsc.wisdomchinaqinghe.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.Login_RegisterActivity;
import com.wsc.wisdomchinaqinghe.activitity.fourmodule.ApplyDetailActivity;
import com.wsc.wisdomchinaqinghe.activitity.fourmodule.ApplyPublishActivity;
import com.wsc.wisdomchinaqinghe.activitity.fourmodule.PensionPublishActivity;
import com.wsc.wisdomchinaqinghe.application.MyApplication;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.ApplyListBean;
import com.wsc.wisdomchinaqinghe.view.MyToast;
import com.wsc.wisdomchinaqinghe.view.iphoneDialogBuilder;

import java.util.List;

/**
 * 楼宇搜索适配器
 * Created by ${wangtao} on 2017/3/14.
 */

public class ApplyDetailListAdapter extends BaseAdapter {
    private Context context;
    private List<ApplyListBean> messageBeen;
    private String cid;
    private MyApplication myApplication;
    private String className;

    public ApplyDetailListAdapter(Context context, List<ApplyListBean> messageBeen,String cid,MyApplication myApplication,String className) {
        this.context = context;
        this.messageBeen = messageBeen;
        this.cid=cid;
        this.myApplication=myApplication;
        this.className=className;
    }

    @Override
    public int getCount() {
        return messageBeen.size();
    }

    @Override
    public Object getItem(int position) {
        return messageBeen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        View view = convertView;
        if (view == null) {
            view= LayoutInflater.from(context).inflate(R.layout.item_applylist_layout,null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        final ApplyListBean applyListBean=messageBeen.get(position);
        holder.item_lyss_textView.setText(applyListBean.getTitle());
        final int apply=applyListBean.getApply();
        if(apply==1)
        {
            holder.tv_shenqing.setTextColor(context.getResources().getColor(R.color.acdd3));
        }else {
            holder.tv_shenqing.setTextColor(context.getResources().getColor(R.color.titelcolor));
        }

        holder.liaojiezhengce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ApplyDetailActivity.class);
                intent.putExtra("id", applyListBean.getId());
                intent.putExtra("cid",cid);
                intent.putExtra("type",applyListBean.getType());
                intent.putExtra("name", applyListBean.getTitle());
                context.startActivity(intent);
            }
        });

        holder.tv_shenqing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(apply==1)
                {
                    boolean isLogin = Tools.getSharedPreferencesboolen(Constant.isLoginIn, false, context);
                    if (isLogin) {
                        int subType=applyListBean.getSubType();
                        int type=applyListBean.getType();
                        if(type==1 && subType==0)
                        {
                            //进入法律援助申请表
                            Intent intent=new Intent(context,ApplyPublishActivity.class);
                            intent.putExtra("type",type);
                            intent.putExtra("subType",subType);
                            intent.putExtra("cid",cid);
                            intent.putExtra("name","法律援助登记表");
                            context.startActivity(intent);
                        }else if(type==10 && subType==0){
                            //进入养老金申请表
                            //进入法律援助申请表
                            Intent intent=new Intent(context,PensionPublishActivity.class);
                            intent.putExtra("type",type);
                            intent.putExtra("subType",subType);
                            intent.putExtra("cid",cid);
                            intent.putExtra("name","尊老金发放审批表(1/2)");
                            context.startActivity(intent);
                        }



                    } else {
                        iphoneDialogBuilder idb = new iphoneDialogBuilder(context);
                        idb.setTitle("提示");
                        idb.setMessage("是否登录");
                        idb.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(context, Login_RegisterActivity.class);
                            myApplication.setSourceIntoLoginRegisterActivity(className);
                            context.startActivity(intent);
                            }
                        });
                        idb.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        idb.show();
                    }
                }else {
                    MyToast myToast=new MyToast(context);
                    myToast.showShort("暂不可申请");
                }

            }
        });
        return view;
    }

    public class ViewHolder {

        private TextView item_lyss_textView;
        private TextView tv_shenqing;
        private TextView liaojiezhengce;

        public ViewHolder(View view) {
            item_lyss_textView = (TextView) view.findViewById(R.id.item_lyss_textView);
            tv_shenqing=(TextView) view.findViewById(R.id.tv_shenqing);
            liaojiezhengce=(TextView) view.findViewById(R.id.liaojiezhengce);
        }
    }
}
